/*
 * @Author: your name
 * @Date: 2021-09-03 07:07:33
 * @LastEditTime: 2021-10-29 00:14:03
 * @LastEditors: Please set LastEditors
 * @Description: 选择类型设置项
 * @FilePath: \phone\src\select_item.h
 */
#ifndef SELECT_ITEM_H
#define SELECT_ITEM_H
// #include "second_page.h"
// #include "item.h"
// #include "page_handle.h"
// #include "interface.h"
#include "select_itemv2.h"
template <typename FUNC>
// class SelectItem : public SecondPage, public Item
class SelectItem : public SelectItemV2
{
public:
    SelectItem(FUNC &value) : data(value)
    {
        SetFuncAmount(static_cast<unsigned int>(FUNC::FUNCTION_END));
        SetSelectData(static_cast<unsigned int>(value));
        // setEvent[static_cast<int>(KeyboardValue::KEY_DIRECTION_LEFT)] = {std::bind(&SelectItem::SelectFunc, this, _1)};
        // setEvent[static_cast<int>(KeyboardValue::KEY_DIRECTION_RIGHT)] = {std::bind(&SelectItem::SelectFunc, this, _1)};
        // setEvent[static_cast<int>(KeyboardValue::KEY_SOFT_KEY_3)] = {std::bind(&SelectItem::SelectFunc, this, nullptr)};
        // setEvent[static_cast<int>(KeyboardValue::KEY_SOFT_KEY_4)] = {std::bind(&SelectItem::SaveData, this, _1)};
    }
    virtual ~SelectItem()
    {
        // Widget *contrl = GetContrlView();
        // if (contrl && !contrl->IsValid())
        // {
        //     LOG_E("contrl view is valid.\n");
        //     return;
        // }
        // std::unique_ptr<Widget> view(GuiEngine::GetInstance().NewWidget());
        // if (GuiEngine::GetInstance().Lookup(contrl, "valueString", true, view.get()))
        // {
        //     GuiEngine::GetInstance().SetSelfLayout(view.get(), "default(w=52%)");
        // }
        // if (GuiEngine::GetInstance().Lookup(contrl, "selectIcon", true, view.get()))
        // {
        //     GuiEngine::GetInstance().Destroy(view.get());
        // }
    }
    std::string &GetDataTrText(FUNC target)
    {
        return trTexts[static_cast<int>(target)];
    }
    // void RefreshPage() override
    // {
    //     RefreshPageCommon(GetString());
    //     /**
    //      * @brief
    //      * 重置一次设置项聚焦属性，否则字符串变化超长时不会滚动。
    //      */
    //     GuiEngine::GetInstance().SetFocus(GetContrlView(), false);
    //     GuiEngine::GetInstance().SetFocus(GetContrlView(), true);
    // }
    // void ResetKeyCmd(KeyCmdMapTable &keyEvent) override
    // {
    //     keyEvent.table[static_cast<int>(KeyboardValue::KEY_DIRECTION_LEFT)] = KeyCmd::KEY_CMD_CONTRL_SECOND_PAGE;
    //     keyEvent.table[static_cast<int>(KeyboardValue::KEY_DIRECTION_RIGHT)] = KeyCmd::KEY_CMD_CONTRL_SECOND_PAGE;
    //     keyEvent.table[static_cast<int>(KeyboardValue::KEY_SOFT_KEY_3)] = KeyCmd::KEY_CMD_CONTRL_SECOND_PAGE;
    //     keyEvent.table[static_cast<int>(KeyboardValue::KEY_SOFT_KEY_4)] = KeyCmd::KEY_CMD_CONTRL_SECOND_PAGE;
    //     keyEvent.table[static_cast<int>(KeyboardValue::KEY_OK)] = KeyCmd::KEY_CMD_CONTRL_SECOND_PAGE;
    //     PageHandle *window = (PageHandle *)Interface::GetInstance().GetCurrentWin();
    //     window->SetKeyEvent(keyEvent);
    // }
    // std::string GetString() override
    // {
    //     char *string = nullptr;
    //     GuiEngine::GetInstance().LocaleInfoTr(trTexts[static_cast<int>(data)].c_str(), &string);
    //     if (!string)
    //     {
    //         LOG_E("can't find any tr text\n");
    //         return "nullptr";
    //     }
    //     return string;
    // }
    // void ItemSelected() override
    // {
    //     Widget *contrl = GetContrlView();
    //     if (contrl && !contrl->IsValid())
    //     {
    //         LOG_E("contrl view is valid.\n");
    //         return;
    //     }
    //     /**
    //      * @brief
    //      * 改变原来的布局。
    //      */
    //     std::unique_ptr<Widget> view6(GuiEngine::GetInstance().NewWidget());
    //     if (GuiEngine::GetInstance().Lookup(contrl, "valueString", true, view6.get()))
    //     {
    //         GuiEngine::GetInstance().SetSelfLayout(view6.get(), "default(w=37%)");
    //         if (GuiEngine::GetInstance().ViewCreate(contrl, 0, 0, 0, 0, view6.get()))
    //         {
    //             /**
    //              * @brief
    //              * 新增一个左右箭头指示。
    //              */
    //             GuiEngine::GetInstance().SetSelfLayout(view6.get(), "default(w=15%)");
    //             GuiEngine::GetInstance().SetName(view6.get(), "selectIcon");
    //             GuiEngine::GetInstance().SetChildrenLayout(view6.get(), "default(r=1,c=0,align_h=center,x=0,y=0,s=0)");
    //             std::unique_ptr<Widget> view2(GuiEngine::GetInstance().NewWidget());
    //             std::unique_ptr<Widget> view3(GuiEngine::GetInstance().NewWidget());
    //             if (GuiEngine::GetInstance().LabelCreate(view6.get(), 0, 0, 0, 0, view3.get()))
    //             {
    //                 GuiEngine::GetInstance().UseStyle(view3.get(), "itemLeft");
    //                 GuiEngine::GetInstance().SetSelfLayout(view3.get(), "default(w=15,h=100%)");
    //             }
    //             if (GuiEngine::GetInstance().LabelCreate(view6.get(), 0, 0, 0, 0, view3.get()))
    //             {
    //                 GuiEngine::GetInstance().SetSelfLayout(view3.get(), "default(w=10,h=100%)");
    //             }
    //             if (GuiEngine::GetInstance().LabelCreate(view6.get(), 0, 0, 0, 0, view3.get()))
    //             {
    //                 GuiEngine::GetInstance().UseStyle(view3.get(), "itemRight");
    //                 GuiEngine::GetInstance().SetSelfLayout(view3.get(), "default(w=15,h=100%)");
    //             }
    //         }
    //     }
    // }
    // void SelectFunc(void *ctx)
    // {
    //     if (ctx)
    //     {
    //         CommonFunc::GetInstance().FunctionSelection(data, ctx);
    //     }
    //     else
    //     {
    //         CommonFunc::GetInstance().FunctionSelection(data, KeyboardValue::KEY_DIRECTION_RIGHT);
    //     }
    //     // CommonFunc::GetInstance().FunctionSelection(data, ctx);
    //     RefreshPageEvent();
    // }
    FUNC GetData()
    {
        return data;
    }
    // void SaveData(void *ctx)
    // {
    //     SecondPage *window = dynamic_cast<SecondPage*>(Interface::GetInstance().GetCurrentWin());
    //     if (window)
    //     {
    //         window->SaveConfiguration();
    //     }
    //     else
    //     {
    //         LOG_E("current window can't save anything.\n");
    //     }
    // }
    std::string GetTrTextFormSettings(unsigned int select) override
    {
        data = static_cast<FUNC>(select);
        return trTexts[select];
    }

private:
    /**
     * @brief 
     * 设置项数据，由派生类初始化
     */
    FUNC &data;
    /**
     * @brief 
     * 设置项译文值，由派生类初始化
     */
    std::string trTexts[static_cast<int>(FUNC::FUNCTION_END)];
};
#endif